package com.example.onion.hxchat.thread;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.util.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * 用于显示用户详细信息的多线程封装
 * @author:onion
 * @data: 2020/7/19
 */
public class PlayUserDetailThread implements Runnable{

    HttpUtils httpUtils = HttpUtils.getInstance();

    String userId;


    private Handler mHandler;
    public static final int DETAIL_IS_FINISH = 1;
    public static final int DETAIL_IS_FAILED = 0;



    /**
     * 构造方法，传入需要显示对象信息的用户id
     * @param userId  用户id
     */
    public PlayUserDetailThread(String userId,Handler handler) {
        this.userId = userId;
        this.mHandler = handler;
    }

    String url = "http://49.232.60.155:8080/AppServer_war/GetUserInfoByIdServlet";
    @Override
    public void run() {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("userId", userId);
        FormBody formBody = builder.build();
        httpUtils.asyncPost(url, formBody, new HttpUtils.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                Message msg = Message.obtain();
                msg.obj = null;
                msg.what = DETAIL_IS_FAILED;
                mHandler.sendMessage(msg);
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onSuccess(Request request, String result) {
                Gson gson = new Gson();
                User user = gson.fromJson(result, (Type) User.class);
                Message msg = Message.obtain();
                msg.obj = user;
                msg.what = DETAIL_IS_FINISH;
                mHandler.sendMessage(msg);
                Log.e("PlayUserDetailThread --->user", user.toString());
            }
        });
    }
}
